clear all
*cap log close
set more off
set seed 603

* estimates for alternative versions of stringency instrument ------------------------------------------------------

* set parameters ------------------------------
local Y  = "cite"
#delimit ;
global cov = "female age agesq age_miss race_b race_h race_o race_u priorprison local
logzip zipincome_miss logprice veh_miss speed_py1 other_py1 crashany_py1" ;
#delimit cr

* setup data ----------------------------------
use "${data}/out/4-main", clear
gen Y = `Y'_ny1
gen D = harsh
label var Z "Leave-out"

gen rank  = _n 
gen ztype = ""
gen n = .
gen rf  = ""
gen rp  = ""
gen bfs = ""
gen sfs = ""
gen ffs = ""
*gen pfs = ""
gen biv = ""
gen siv = ""
gen biv2 = ""
gen siv2 = ""

* compute alternative instruments -------------------
// pre-residualized (ala Dobbie) -------------
reghdfe D, absorb(totfe) resid 
predict rD, resid 
bysort officerid: egen sumD = sum(rD)
bysort officerid: egen sumN = sum(1)
gen Zr = (sumD-rD)/(sumN-1)
drop sumD sumN
label var Zr "Leave-Out (Residualized)"

// leave-county out -------------
bysort officerid countynum: egen sumDc = sum(D)
bysort officerid countynum: egen sumNc = sum(1)
bysort officerid: egen sumD = sum(D)
bysort officerid: egen sumN = sum(1)
gen Zc = (sumD-sumDc)/(sumNc-sumN)
drop sumDc sumNc sumD sumN 
label var Zc "Leave-County-Out"

// binary ------------------------
gen Zb = 1-lenientpart
label var Zb "Binary"

// estimated officer effects -----
merge m:1 officerid using "${est}/getfe", keep(1 3) nogen 
gen Zf = est 
label var Zf "Officer Dummies"
sort rank 


* compute IV estimates with alt instruments --------------
local k = 1 
foreach Z in Z Zr Zc Zb Zf {

	local lab: variable label `Z'
	replace ztype = "`lab'" if _n == `k'

	* randomization test ----------------
	reghdfe `Z' ${cov}, absorb(totfe) vce(cluster officerid)
	test ${cov} 
	replace rf = "`:di %5.3f r(F)'" if _n == `k'
	local p = "p = `:di %5.4f r(p)'"
	if `r(p)' < 0.0001 {
		local p = "p < 0.0001"
	}
	replace rp = "`p'" if _n == `k'

	* first stage --------------------
	reghdfe D `Z', absorb(totfe) vce(cluster officerid)
	replace bfs = "`:di %5.4f _b[`Z']'" if _n == `k'
	replace sfs = "`:di %5.4f _se[`Z']'" if _n == `k'
	test `Z'
	replace ffs = "`:di %6.0f `r(F)''" if _n == `k'

	* iv -------------------------------
	ivreghdfe Y (D=`Z'), absorb(totfe) vce(cluster officerid)
	replace biv = "`:di %5.4f  _b[D]'" if _n == `k'
	replace siv = "`:di %5.4f _se[D]'" if _n == `k'
	replace n = e(N) if _n == `k'

	* iv with controls --------------
	ivreghdfe Y (D=`Z') ${cov}, absorb(totfe) vce(cluster officerid)
	replace biv2 = "`:di %5.4f  _b[D]'" if _n == `k'
	replace siv2 = "`:di %5.4f _se[D]'" if _n == `k'

	local ++k
}	

* fix officer dummies
* first stage F-stat reported in output/apx_iv/rsquared.txt
replace ffs = "323" if ztype=="Officer Dummies"


* compute alt instruments: within groups -------------------------
// vars for groups -----------------
gen     temprace = race
replace temprace = "OU" if race=="O"|race=="U"
summ age, d
gen old  = age>r(p50)
summ zipincome, d
gen rich = zipincome>r(p50)

// define groups ------------------
egen grp1 = group(temprace)
egen grp2 = group(temprace female old)
egen grp3 = group(temprace female old cite_py1)
egen grp4 = group(temprace female old cite_py1 rich)

// instruments --------------------
forval g = 1/4 {
	bysort officerid grp`g': egen sumD = sum(D) 
	bysort officerid grp`g': egen sumN = sum(1)
	gen Z`g' = (sumD-D)/(sumN-1)

	replace Z`g' = . if sumN<10
	drop sumD sumN
}

label var Z1 "Race"
label var Z2 "Race $\times$ Gender $\times$ Age"
label var Z3 "Race $\times$ Gender $\times$ Age $\times$ History"
label var Z4 "Race $\times$ Gender $\times$ Age $\times$ History $\times$ Income"
sort rank 


* compute IV estimates with group instruments ---------------------
forval g = 1/4 {
	
	local lab: variable label Z`g'
	replace ztype = "`lab'" if _n == `k'

	* randomization test ----------------
	reghdfe Z`g' ${cov}, absorb(totfe grp`g') vce(cluster officerid)
	test ${cov}
	replace rf = "`:di %5.3f r(F)'" if _n == `k'
	local p = "p = `:di %5.4f r(p)'"
	if `r(p)' < 0.0001 {
		local p = "p < 0.0001"
	}
	replace rp = "`p'" if _n == `k'

	* first stage -------------------
	reghdfe D Z`g', absorb(totfe grp`g') vce(cluster officerid)
	replace bfs = "`:di %5.4f _b[Z`g']'" if _n == `k'
	replace sfs = "`:di %5.4f _se[Z`g']'" if _n == `k'
	test Z`g'
	replace ffs = "`:di %6.0f `r(F)''" if _n == `k'

	* iv ---------------------------
	ivreghdfe Y (D=Z`g'), absorb(totfe grp`g') vce(cluster officerid)
	replace biv = "`:di %5.4f  _b[D]'" if _n == `k'
	replace siv = "`:di %5.4f _se[D]'" if _n == `k'
	replace n = e(N) if _n == `k'

	* iv with controls -------------
	ivreghdfe Y (D=Z`g') ${cov}, absorb(totfe grp`g') vce(cluster officerid)
	replace biv2 = "`:di %5.4f  _b[D]'" if _n == `k'
	replace siv2 = "`:di %5.4f _se[D]'" if _n == `k'

	local ++k
}	


* add parens to se -------
replace siv  = "("+siv+")" if !mi(siv)
replace siv2 = "("+siv2+")" if !mi(siv2)

* post output into LaTeX table ------------------------
capture erase "${out}/apx_deter/table_instruments.tex"
file open fh using "${out}/apx_deter/table_instruments.tex", write replace

local instr = "Main Resid County Binary Dummies"	
local demos = "R RGA RGAH RGAHI"
file write fh ///	
	"& & \multicolumn{2}{c}{\$F\$-Stat} && \multicolumn{1}{c}{}    \\  \cline{3-4}" _n /// 
	"&\multicolumn{1}{c}{(1)}&\multicolumn{1}{c}{(2)}&\multicolumn{1}{c}{(3)}&&\multicolumn{1}{c}{(4)} \\" _n ///
	"{Instrument}&\multicolumn{1}{c}{N}&\multicolumn{1}{c}{Balance}&\multicolumn{1}{c}{FS}&&\multicolumn{1}{c}{$\beta_{IV}$} \\" _n ///
	"\hline" _n

local k=1
foreach y in `instr' {

	local label = ztype[`k']
	local Ncount = n[`k']
	local m1 = rf[`k']
	local m2 = ffs[`k']
	local m3 = biv2[`k']
	*local m3 = siv[`k']
	*local m4 = writeb_other[`k']
	*local m5 = writeb_crash[`k']

	file write fh "`label' & `Ncount' &`m1' & `m2' && `m3' \\" _n

	local m3 = siv2[`k']
	*local m4 = writes_other[`k']
	*local m5 = writes_crash[`k']

	file write fh "&  &  &  && `m3'  \\" _n
	file write fh "[1em]" _n 
	local ++k
}

file write fh ///
	"\multicolumn{6}{l}{\textit{\underline{Within Demographics}}}  \\"  _n ///

foreach y in `demos' {

local label = ztype[`k']
local Ncount = n[`k']
local m1 = rf[`k']
local m2 = ffs[`k']
local m3 = biv2[`k']
*local m4 = writeb_other[`k']
*local m5 = writeb_crash[`k']

file write fh "\hspace{3mm}`label'& `Ncount'  & `m1' & `m2' && `m3' \\" _n

local m3 = siv2[`k']
*local m4 = writes_other[`k']
*local m5 = writes_crash[`k']

file write fh "&  &  &  && `m3' \\" _n
file write fh "[1em]" _n 

local ++k
}

file write fh "\bottomrule" _n  

file close fh
macro drop fh
* ------------------------------------------------------------------------------------------------------------------




